package com.aspire.core.match.ua.tongji;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.aspire.core.match.ua.UACenter;
import com.aspire.core.match.ua.object.Device;
import com.aspire.core.match.ua.object.DeviceUA;

public class Tongji0703 {

	public static void main(String[] args) throws IOException {
//		ApplicationContext c1 = new ClassPathXmlApplicationContext(
//				new String[] { "applicationContext_database.xml",
//						"applicationContext_sharecommon.xml" });
//
//		UACenter uaCenter = (UACenter) c1.getBean("mmUACenter");
//		//Device device = uaCenter.matchUA("MT810");
//		
//		String ua = "GiONEE-GN700T_TD/v1 Linux/3.0.13 Android/4.0.4 Release/10.24.2012 Browser/AppleWebKit534.30/Mobile Safari/534.30";
//		
//		ua = "HUAWEI Y310";
//		
//		System.out.println( uaCenter.matchUA(ua) );
		
		//System.out.println( uaCenter.searchBrand("小米") );
//		System.out.println( uaCenter.getDevice(7888) );
		
//		Map<Integer, List<DeviceUA>> deviceUaMap = uaCenter.getNewUACache()
//				.getDeviceUAMap();
//
//		List<String> uaList = FileUtils.readLines(new File("G:\\ua\\0703.txt"));
//
//		Iterator<String> ite = uaList.iterator();
//
//		StringBuffer sb = new StringBuffer();
//		while (ite.hasNext()) {
//			String uaLine = ite.next();
//			String ua = StringUtils.substringAfterLast(uaLine, "	");
//			
//			int deviceid = matchUA(ua, deviceUaMap);
//			Device device = null;
//			
//			if( deviceid == -1 )
//			{
//				device = uaCenter.matchUA(ua);
//			}
//			else
//			{
//				device = uaCenter.getDevice(deviceid);
//			}
//			
//			if( device != null )
//			{
//				sb.append(uaLine + "," + device.getDevice_id() + "," + device.getDevice_name() + "," + device.getDevice_desc());
//			}
//			else
//			{
//				sb.append(uaLine + ",null,null,null");
//			}
//			
//			sb.append("\n");
//		}
//		
//		FileUtils.writeStringToFile(new File("G:\\zsx.txt"), sb.toString());
	}

	public static int matchUA(String ua,
			Map<Integer, List<DeviceUA>> deviceUaMap) {
		Iterator<Integer> ite = deviceUaMap.keySet().iterator();

		while (ite.hasNext()) {
			int code = ite.next();

			List<DeviceUA> deviceUAList = deviceUaMap.get(code);

			for (int i = 0; i < deviceUAList.size(); i++) {
				DeviceUA deviceUA = deviceUAList.get(i);
				if (StringUtils
						.equalsIgnoreCase(ua, deviceUA.getDevice_os_ua())
						|| StringUtils.equalsIgnoreCase(ua,
								deviceUA.getDevice_ua())) {
					return code;
				}
			}
		}

		return -1;
	}
}
